package cn.tedu.ivos.vehicle.service.impl;

import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.AddVehicleParam;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVo;
import cn.tedu.ivos.vehicle.service.VehicleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class VehicleServiceImpl implements VehicleService {
    @Autowired
    VehicleMapper vehicleMapper;
    @Override
    public List<VehicleVo> selectVehicleTableSer(VehicleQuery vehicleQuery) {
        List<VehicleVo> vehicleVoList =vehicleMapper.selectVehicleTableMap(vehicleQuery);
        return vehicleVoList;
    }

    @Override
    public int addVehicleSer(AddVehicleParam addVehicleParam) {
        if(addVehicleParam!=null){
            log.debug("addVehicleParam:{}",addVehicleParam);
            Vehicle vehicle = new Vehicle();
            BeanUtils.copyProperties(addVehicleParam,vehicle);
            if(vehicle.getId()==null){
                vehicle.setCreateTime(new Date());
                vehicle.setStatus("1");
                vehicle.setGeofenceBindStatus("0");
                int rows = vehicleMapper.insertVehicle(vehicle);
                return rows;
            }else {
                vehicle.setUpdateTime(new Date());
                int rows = vehicleMapper.updateVehicle(vehicle);
                return 0;
            }

        }
        return 0;
    }

    @Override
    public int deleteVehicleSer(Long id) {
        int row = vehicleMapper.deleteVehicleById(id);
        return row;
    }

    @Override
    public int unbindVehicleSer(Long vehicleId) {
        if(vehicleId!=null){
            Vehicle vehicle = new Vehicle();
            vehicle.setId(vehicleId);
            vehicle.setGeofenceBindStatus("0");
            vehicle.setUpdateTime(new Date());
            int row = vehicleMapper.unbindVehicle(vehicle);
            return row;
        }
        return 0;
    }

    @Override
    public int bindVehicleSer(Long vehicleId, Long geofenceId) {
        if(vehicleId!=null&&geofenceId!=null){
            Vehicle vehicle = new Vehicle();
            vehicle.setId(vehicleId);
            vehicle.setGeofenceBindStatus("1");
            vehicle.setGeofenceId(geofenceId);
            vehicle.setUpdateTime(new Date());
            int row = vehicleMapper.updateVehicle(vehicle);
            return row;
        }
        return 0;
    }
}
